import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'
import { immer } from 'zustand/middleware/immer'
import { IdentityType } from '../types/storeTypes'
import { UsrType } from '@/types/sysTypes'
import { createSelectors } from './createSelector'

export const useIdentityStore = createSelectors(
    create<IdentityType>()(
        immer(
            persist((set) => ({
                info: {
                    id: '',
                    token: '',
                },
                setInfo: (info: UsrType) => set({ info }),
                removeInfo: () => set(() => ({
                    info: {
                        id: '',
                        token: '',
                    }
                })),
                }),{
                    name: 'identity',
                    storage: createJSONStorage(() => sessionStorage),
                }
            )
        )
    )
)

